package com.myapp.ruyixing.ui.shopmanager;

import static android.view.View.VISIBLE;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ActivityOrderList;
import com.myapp.ruyixing.model.ActivityOrderList.Order;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;

// 参与者管理
public class ParticipantManageActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {
	private int mCurrentTab = 0;
	private String mActivityId;

	private List<ActivityOrderList.Order> mList;
	private MyListAdapter mAdapter;

	private TitleBar mTitleBar;
	private View mLoadingView;
	private ListView mListView;
	private TextView mEmptyView;
	private View mFocusedTabView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				ActivityOrderList list = (ActivityOrderList) o;
				onLoadingDataFinished(true, list);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String shopbduss, String aid, int status,
			int pn, int rn) {
		showLoading();
		RYHttpClient.getActivityOrderList(this, mHttpResponseHandler,
				shopbduss, aid, status + "", pn, rn);
	}

	private void onLoadingDataFinished(boolean succeed,
			ActivityOrderList orderList) {
		hideLoading();
		if (succeed && orderList != null && orderList.items != null) {
			mList.clear();
			mEmptyView.setVisibility(View.GONE);
			mList.addAll(orderList.items);
			mAdapter.notifyDataSetChanged();
			if (mList.size() == 0) {
				mEmptyView.setVisibility(VISIBLE);
			}
		} else {
			Toast.makeText(this,
					"加载失败" + (orderList == null ? "" : orderList.msg),
					Toast.LENGTH_SHORT).show();
			mEmptyView.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_participant_manage);

		initView();

		mList = new ArrayList<ActivityOrderList.Order>();
		mAdapter = new MyListAdapter();
		mListView.setAdapter(mAdapter);

		mActivityId = getIntent().getStringExtra("aid");
	}

	@Override
	protected void onResume() {

		onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
				mActivityId, mCurrentTab, 0, 50);
		super.onResume();
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mListView.setOnItemClickListener(this);

		initTab();

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		((EditText) findViewById(R.id.edit_search))
				.setOnEditorActionListener(new OnEditorActionListener() {

					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						// TODO 搜索
						return false;
					}
				});
	}

	private void initTab() {
		mFocusedTabView = findViewById(R.id.about_to_check);
		mFocusedTabView.setOnClickListener(this);
		mFocusedTabView.setSelected(true);

		findViewById(R.id.check_pass).setOnClickListener(this);
		findViewById(R.id.check_not_pass).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.about_to_check:
			mCurrentTab = 0;
			if (changeTab(v)) {
				mList.clear();
				mAdapter.notifyDataSetChanged();
				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						mActivityId, 0, 0, 50);
			}
			break;
		case R.id.check_pass:
			mCurrentTab = 1;
			if (changeTab(v)) {
				mList.clear();
				mAdapter.notifyDataSetChanged();
				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						mActivityId, 1, 0, 50);
			}
			break;
		case R.id.check_not_pass:
			mCurrentTab = 2;
			if (changeTab(v)) {
				mList.clear();
				mAdapter.notifyDataSetChanged();
				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						mActivityId, 2, 0, 50);
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		startActivity(new Intent(this, ParticipantDetailActvity.class)
				.putExtra("dataid", mList.get(position).dataid).putExtra("aid",
						mActivityId));
	}

	private boolean changeTab(View v) {
		if (mFocusedTabView == v) {
			return false;
		}

		mFocusedTabView.setSelected(false);
		mFocusedTabView = v;
		mFocusedTabView.setSelected(true);
		return true;
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	private class ViewHolder {
		private TextView mNameView;
		private TextView mTimeView;
		private TextView mPhoneView;
	}

	private class MyListAdapter extends BaseAdapter {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyListAdapter() {
			super();
			mQueue = Volley.newRequestQueue(ParticipantManageActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public int getCount() {
			return mList == null ? 0 : mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(
						ParticipantManageActivity.this).inflate(
						R.layout.cell_shop_manager_participant_manage, parent,
						false);

				ViewHolder holder = new ViewHolder();
				holder.mNameView = (TextView) convertView
						.findViewById(R.id.name);
				holder.mTimeView = (TextView) convertView
						.findViewById(R.id.time);
				holder.mPhoneView = (TextView) convertView
						.findViewById(R.id.phone);

				convertView.setTag(holder);
			}
			Order item = mList.get(position);

			ViewHolder mHolder = (ViewHolder) convertView.getTag();

			mHolder.mNameView.setText("姓名：" + item.name);
			mHolder.mPhoneView.setText("预订电话：" + item.phone);
			mHolder.mTimeView.setText(item.datetime);

			return convertView;

		}
	}
}
